Skip to content

Commit e586cec

Browse files
authored
Merge pull request #6681 from escattone/get-products-2348
helper function that provides product info
2 parents 99a850a + 4770cdf commit e586cec

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

kitsune/products/tests/test_utils.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from kitsune.products.tests import ProductFactory, TopicFactory
2-
from kitsune.products.utils import get_taxonomy
2+
from kitsune.products.utils import get_products, get_taxonomy
33
from kitsune.sumo.tests import TestCase
44

55

@@ -408,3 +408,65 @@ def test_specific_product_with_metadata(self):
408408
),
409409
expected,
410410
)
411+
412+
413+
class GetProductsTests(TestCase):
414+
415+
def setUp(self):
416+
ProductFactory(
417+
title="product1", description="All about product1...", display_order=1, slug="p1"
418+
)
419+
ProductFactory(
420+
title="product2", description="All about product2...", display_order=2, slug="p2"
421+
)
422+
ProductFactory(
423+
title="product3",
424+
description="All about product3...",
425+
display_order=3,
426+
slug="mozilla-account",
427+
visible=False,
428+
)
429+
ProductFactory(
430+
title="product4",
431+
description="All about product4...",
432+
display_order=4,
433+
slug="p4",
434+
visible=False,
435+
)
436+
ProductFactory(
437+
title="product5",
438+
description="All about product5...",
439+
display_order=5,
440+
slug="p5",
441+
is_archived=True,
442+
)
443+
444+
def test_get_products(self):
445+
expected = """products:
446+
- title: product1
447+
description: All about product1...
448+
- title: product2
449+
description: All about product2...
450+
- title: product3
451+
description: All about product3...
452+
"""
453+
self.assertEqual(get_products(), expected)
454+
455+
def test_get_products_as_json(self):
456+
expected = """{
457+
"products": [
458+
{
459+
"title": "product1",
460+
"description": "All about product1..."
461+
},
462+
{
463+
"title": "product2",
464+
"description": "All about product2..."
465+
},
466+
{
467+
"title": "product3",
468+
"description": "All about product3..."
469+
}
470+
]
471+
}"""
472+
self.assertEqual(get_products(output_format="JSON"), expected)

kitsune/products/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,25 @@ def get_topics(parent=None):
7979
return json.dumps(result, indent=2, sort_keys=False)
8080

8181
return yaml.dump(result, indent=2, sort_keys=False)
82+
83+
84+
def get_products(output_format: str = "YAML") -> str:
85+
"""
86+
Returns the currently active products, each with their title and description,
87+
as a string in the output format requested (either "YAML" or "JSON").
88+
"""
89+
products: list[dict[str, str]] = []
90+
result = dict(products=products)
91+
92+
for product in Product.active.filter(Q(visible=True) | Q(slug="mozilla-account")):
93+
products.append(
94+
dict(
95+
title=product.title,
96+
description=product.description,
97+
)
98+
)
99+
100+
if output_format.lower() == "json":
101+
return json.dumps(result, indent=2, sort_keys=False)
102+
103+
return yaml.dump(result, indent=2, sort_keys=False)

0 commit comments

Comments
 (0)